package com.hz.wjy.controller;

import com.hz.wjy.model.entiy.UserEntity;
import com.hz.wjy.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.jms.Queue;

@RestController
public class UserController {

    @Resource
    UserService userService;

    @Resource
    JmsMessagingTemplate jmsMessagingTemplate;

    @Resource
    Queue queue;

    @PostMapping("/user/add")
    public String addUser(@RequestParam("username") String username, @RequestParam("realname") String realname,
                          @RequestParam("schoolId") String studentId, @RequestParam("password") String password,
                          @RequestParam("userType") int userType) {
        if (userService.isLogined(username)) {

        }
        userService.addUser(username, realname, studentId, password, userType);
        return "success";
    }

    @GetMapping("/user/islogin")
    public String checkLogined(@RequestParam("username") String username) {
        return userService.isLogined(username) ? "yes" : "false";
    }

    @PostMapping("/user/login")
    public String login(@RequestParam("username") String username, @RequestParam("password") String password) {

        // 尝试登录日志
        jmsMessagingTemplate.convertAndSend(queue, username + " try to  login");

        return userService.login(username, password) ? "login success" : "error username or password";
    }

}
